package com.campus.client.view;

import com.campus.client.controller.StudentController;
import com.campus.client.controller.TeacherController;
import com.campus.client.net.ClientConnector;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;

import java.io.IOException;

public class TeacherRegisterFrame {

    private TextField nameField=new TextField();
    private TextField ageField=new TextField();
    private TextField contactField=new TextField();
    private TextField genderField=new TextField();
    private TextField departmentField=new TextField();
    private Button registerBtn = new Button("注册");;
    private Button backBtn = new Button("返回");
    private String teacherId;
    private String password;

    private TeacherController controller;

    public String getTeacherId() {
        return teacherId;
    }
    public String getPassword() {
        return password;
    }

    public void show(Stage primaryStage,String username,String password) throws IOException {

        this.teacherId=username;
        this.password=password;
        // 创建主VBox容器
        VBox mainVBox = new VBox();

        ClientConnector connector = new ClientConnector("172.20.10.2", 8888);
        controller = new TeacherController(this, connector);

        mainVBox.setPrefHeight(400.0);
        mainVBox.setPrefWidth(640.0);

        // 创建AnchorPane
        AnchorPane anchorPane = new AnchorPane();
        anchorPane.setMaxHeight(Double.NEGATIVE_INFINITY);
        anchorPane.setMaxWidth(Double.NEGATIVE_INFINITY);
        anchorPane.setPrefHeight(651.0);
        anchorPane.setPrefWidth(719.0);
        AnchorPane.setTopAnchor(anchorPane, 0.0);
        AnchorPane.setBottomAnchor(anchorPane, 0.0);
        AnchorPane.setLeftAnchor(anchorPane, 0.0);
        AnchorPane.setRightAnchor(anchorPane, 0.0);

        // 创建顶部蓝色矩形
        Rectangle topRect = new Rectangle();
        topRect.setArcHeight(5.0);
        topRect.setArcWidth(5.0);
        topRect.setFill(Color.DODGERBLUE);
        topRect.setHeight(83.0);
        topRect.setStroke(Color.BLACK);
        topRect.setStrokeType(javafx.scene.shape.StrokeType.INSIDE);
        topRect.setWidth(640.0);

        // 创建标题文本
        Text titleText = new Text("教师注册账号");
        titleText.setFill(Color.WHITE);
        titleText.setLayoutX(211.0);
        titleText.setLayoutY(55.0);
        titleText.setStrokeWidth(0.0);
        titleText.setWrappingWidth(217.99998474121094);
        titleText.setFont(Font.font("System", FontWeight.BOLD, 36.0));

        // 创建主内容HBox
        HBox mainHBox = new HBox();
        mainHBox.setLayoutX(30.0);
        mainHBox.setLayoutY(115.0);
        mainHBox.setPrefHeight(219.0);
        mainHBox.setPrefWidth(580.0);
        mainHBox.setSpacing(30.0);

        // 创建左侧VBox
        VBox leftVBox = new VBox();
        leftVBox.setPrefHeight(245.0);
        leftVBox.setPrefWidth(268.0);
        leftVBox.setSpacing(20.0);

        // 姓名HBox
        HBox nameHBox = new HBox();
        nameHBox.setPrefHeight(36.0);
        nameHBox.setPrefWidth(100.0);

        Text nameLabel = new Text("姓名：");
        nameLabel.setStrokeWidth(0.0);
        nameLabel.setWrappingWidth(75.99998474121094);
        nameLabel.setFont(Font.font(24.0));

        nameField.setPrefHeight(31.0);
        nameField.setPrefWidth(191.0);

        nameHBox.getChildren().addAll(nameLabel, nameField);

        // 年龄HBox
        HBox ageHBox = new HBox();
        ageHBox.setPrefHeight(34.0);
        ageHBox.setPrefWidth(100.0);

        Text ageLabel = new Text("年龄：");
        ageLabel.setStrokeWidth(0.0);
        ageLabel.setWrappingWidth(75.99998474121094);
        ageLabel.setFont(Font.font(24.0));

        ageField.setPrefHeight(31.0);
        ageField.setPrefWidth(191.0);

        ageHBox.getChildren().addAll(ageLabel, ageField);

        // 联系方式HBox
        HBox contactHBox = new HBox();
        contactHBox.setPrefHeight(38.0);
        contactHBox.setPrefWidth(135.0);

        Text contactLabel = new Text("联系方式：");
        contactLabel.setStrokeWidth(0.0);
        contactLabel.setWrappingWidth(120.66661071777344);
        contactLabel.setFont(Font.font(24.0));

        contactField.setPrefHeight(31.0);
        contactField.setPrefWidth(148.0);

        contactHBox.getChildren().addAll(contactLabel, contactField);

        // 将左侧所有组件添加到leftVBox
        leftVBox.getChildren().addAll(nameHBox, ageHBox, contactHBox);

        // 创建右侧VBox
        VBox rightVBox = new VBox();
        rightVBox.setPrefHeight(263.0);
        rightVBox.setPrefWidth(280.0);
        rightVBox.setSpacing(20.0);

        // 性别HBox
        HBox genderHBox = new HBox();
        genderHBox.setAlignment(Pos.CENTER_LEFT);
        genderHBox.setPrefHeight(26.0);
        genderHBox.setPrefWidth(411.0);

        Text genderLabel = new Text("性别：");
        genderLabel.setStrokeWidth(0.0);
        genderLabel.setWrappingWidth(75.99998474121094);
        genderLabel.setFont(Font.font(24.0));

        genderField.setPrefHeight(31.0);
        genderField.setPrefWidth(215.0);

        genderHBox.getChildren().addAll(genderLabel, genderField);

        // 部门HBox
        HBox departmentHBox = new HBox();
        departmentHBox.setAlignment(Pos.CENTER_LEFT);
        departmentHBox.setPrefHeight(35.0);
        departmentHBox.setPrefWidth(359.0);

        Text departmentLabel = new Text("部门：");
        departmentLabel.setStrokeWidth(0.0);
        departmentLabel.setWrappingWidth(75.3332970738411);
        departmentLabel.setFont(Font.font(24.0));

        departmentField.setPrefHeight(31.0);
        departmentField.setPrefWidth(201.0);

        departmentHBox.getChildren().addAll(departmentLabel, departmentField);

        // 将右侧所有组件添加到rightVBox
        rightVBox.getChildren().addAll(genderHBox, departmentHBox);

        // 将左右VBox添加到主HBox
        mainHBox.getChildren().addAll(leftVBox, rightVBox);

        // 创建注册按钮
        registerBtn.setLayoutX(496.0);
        registerBtn.setLayoutY(334.0);
        registerBtn.setMnemonicParsing(false);
        registerBtn.setPrefHeight(30.0);
        registerBtn.setPrefWidth(107.0);
        registerBtn.setStyle("-fx-background-color: #1E90FF;");
        registerBtn.setTextFill(Color.WHITE);
        registerBtn.setFont(Font.font(24.0));

        // 创建返回按钮
        backBtn.setLayoutX(364.0);
        backBtn.setLayoutY(334.0);
        backBtn.setMnemonicParsing(false);
        backBtn.setPrefHeight(30.0);
        backBtn.setPrefWidth(107.0);
        backBtn.setStyle("-fx-background-color: #1E90FF;");
        backBtn.setTextFill(Color.WHITE);
        backBtn.setFont(Font.font(24.0));

        // 将所有组件添加到AnchorPane
        anchorPane.getChildren().addAll(topRect, titleText, mainHBox, backBtn, registerBtn);

        // 将AnchorPane添加到主VBox
        mainVBox.getChildren().add(anchorPane);

        // 创建场景并显示
        Scene scene = new Scene(mainVBox);
        primaryStage.setTitle("教师注册");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public String getName(){return nameField.getText();}
    public String getAge(){return ageField.getText();}
    public String getGender(){return genderField.getText();}
    public String getContact(){return contactField.getText();}
    public String getDepartment(){return departmentField.getText();}

    public void setRegisterButtonAction(java.util.function.Consumer<javafx.event.ActionEvent> handler) {
        registerBtn.setOnAction(handler::accept);
    }

    public void setBackButtonAction(java.util.function.Consumer<javafx.event.ActionEvent> handler) {
        backBtn.setOnAction(handler::accept);
    }

    public void showLoginFrame() throws IOException {
        // 获取当前登录窗口的Stage
        Stage registerStage = (Stage) registerBtn.getScene().getWindow();

        // 创建主界面
        LoginFrame loginFrame = new LoginFrame();
        Stage loginStage = new Stage();

        // 显示主界面
        loginFrame.start(loginStage);

        // 关闭登录窗口
        registerStage.close();
    }

}